package metier;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;

import presentation.EquipeInterface;

import com.mysql.jdbc.ResultSet;

import database.Database;
/**
 * Class Equipe
 * Package metier
 * Dfinition d'une Equipe
 * 
 * @author Herv Le Fouler et Lionel Lasry
 */
public class Equipe  extends UnicastRemoteObject  implements EquipeInterface {
	private String nom;
	private int IdChampionnat;
	private int id;
	private int points;
	private int goalaverage;
	
	/**
	 * Renvoit le nombre de points actuels d'une quipe
	 * @return int points
	 */
	public int getPoints() throws RemoteException {
		return points;
	}
	/**
	 * Renvoit le goalaverage actuel d'une quipe
	 * @return int points
	 */
	public int getGoalaverage()  throws RemoteException{
		return goalaverage;
	}
	/**
	 * Constructeur de Equipe
	 */
	public Equipe()  throws RemoteException {
		this.nom = nom;
	}
	/**
	 * Constructeur de Championnat
	 * @param int idChampionnat le numero id du championnat
	 * @param String nom de l'quipe
	 */
	public Equipe(int idChampionnat, String nom) throws RemoteException{
		this.IdChampionnat = IdChampionnat;
		this.nom = nom;
	}
	/**
	 * Constructeur de Equipe  partir de son id
	 * Rcupre tout les informations d'une quipe  partir de son id
	 * et instancie l'objet avec ses valeurs
	 * @param int id_equipe
	 * @deprecated Remplac par findEquipe(int id_equipe)
	 */
	public Equipe(int id_equipe)  throws RemoteException{
		Database database = new Database();
		database.createConnection();
		try{
			ResultSet res = database.getAttributesById(id_equipe,"equipe");
			res.next();
			this.nom = res.getString("nom");
			this.points = res.getInt("points");
			this.goalaverage = res.getInt("goalaverage");
		}
		catch (SQLException ex){ex.printStackTrace ();}
	}
	/**
	 * Insre une quipe dans la base si elle n'existe pas dj
	 * @param int idChampionnat le numero id du championnat
	 * @param String nom de l'quipe
	 * @return boolean : true si l'insertion a t faite, false si l'quipe existe dj
	 */
	public boolean createEquipe(int IdChampionnat, String nom) throws RemoteException{
		Database database = new Database();
		database.createConnection();
		ResultSet res = this.findAllEquipes(IdChampionnat);
		try {
			while (res.next()){//tant qu'il y'a des lignes
				if (nom.compareTo(res.getString("nom")) ==  0){
					return false;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		database.lancerUpdate("Insert into equipe (id,id_championnat,nom) Values (NULL,"+IdChampionnat+",'"+nom+"')");
		database.closeConnection();
		return true;
	}
	/**
	 * Rcupre tout les informations d'une quipe  partir de son nom
	 * et instancie l'objet avec ses valeurs
	 * @param String nameEquipe : nom de l'quipe
	 */
	public void findEquipe(String nameEquipe) throws RemoteException{
		this.nom = nameEquipe;
		Database database = new Database();
		database.createConnection();
		ResultSet res = database.returnResultOfQuery("SELECT * from Equipe E where E.nom = '"+nameEquipe+"'");
		try {
			res.next();
			this.id = res.getInt("id");
			this.IdChampionnat = res.getInt("id_championnat");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		database.closeConnection();
	}
	/**
	 * Constructeur de Equipe  partir de son id
	 * Rcupre tout les informations d'une quipe  partir de son id
	 * et instancie l'objet avec ses valeurs
	 * @param int idEquipe l'id de l'quipe
	 */
	public void findEquipeById(int idEquipe) throws RemoteException{
		this.id = idEquipe;
		Database database = new Database();
		database.createConnection();
		ResultSet res = database.returnResultOfQuery("SELECT * from Equipe E where E.id = '"+idEquipe+"'");
		try {
			res.next();
			this.nom = res.getString("nom");
			this.IdChampionnat = res.getInt("id_championnat");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		database.closeConnection();
	}
	/**
	 * Cherche l'ensemble des quipes enregistres dans un championnat
	 * @param int id_championnat l'id du championnat dont on cherche les quipes
	 * @return ResultSet un resultSet contenant toutes les quipes
	 */
	public ResultSet findAllEquipes(int id_championnat) {
		Database database = new Database();
		database.createConnection();
		ResultSet res = database.returnResultOfQuery("SELECT * from Equipe where id_championnat = "+id_championnat);
		return res;
	}
	/**
	 * Renvoit le nom d'une equipe
	 * @return String nom
	 */
	public String getNom()  throws RemoteException {
		return nom;
	}
	/**
	 * Renvoit l'id d'une quipe
	 * @return int id
	 */
	public int getId()  throws RemoteException {
		return id;
	}
}
